#!/bin/bash

# 开发模式启动方式:
# 1. gunicorn cloudlte.wsgi:application -c gunicorn.conf.py
# 2. python manage.py runserver 0.0.0.0:7000
# 3. uwsgi --ini cloudlte_uwsgi.ini

# 定义输出颜色 
SETCOLOR_SUCCESS="\E[1;32m"
SETCOLOR_FAILURE="\E[1;31m"
SETCOLOR_WARNING="\E[1;33m"
SETCOLOR_NORMAL="\E[0;39m"
SETCOLOR_SUCCESS_SHARE='\E[32;5m'
SETCOLOR_RES='\E[0m'
SHAN='\E[33;5m'

# 获取本机IP地址
# 获取主机名
hostname=$(hostname)
# 使用 awk 获取 IP 地址
ip_address=$(hostname -I | awk '{print $1}')
# 获取cloudlte_uwsgi.ini中指定的启动端口
port=$(sed -n '15p' cloudlte_uwsgi.ini | cut -d':' -f2) 


# args
if [ $# -ne 1 ]; then
    echo -e "${SETCOLOR_WARNING}Usage: $0 start|restart|stop|status ${SETCOLOR_RES}"
    source .env/bin/activate
    isrun=`ps -ef|grep [c]loudlte_uwsgi|wc -l`
    if [ $isrun -gt 0 ]; then
        echo -e "${SETCOLOR_WARNING}CLoudLte Status: ${SETCOLOR_SUCCESS}Starting. ${SETCOLOR_RES}"
        echo -e "Develop Server at http://${ip_address}:${port}/"
    else
        echo -e "${SETCOLOR_WARNING}CLoudLte Status: ${SETCOLOR_FAILURE}Stoped. ${SETCOLOR_RES}"
    fi
    exit 1
fi

# start
if [ $1 == 'start' ]; then
    isrun=`ps -ef|grep [c]loudlte_uwsgi|wc -l`
    if [ $isrun -gt 0 ]; then
        echo -e "${SETCOLOR_SUCCESS_SHARE}The CloudLte is Start, \n Develop Server at http://${ip_address}:${port}/ ${SETCOLOR_RES}"
        read -n 1 -s
        tail -f .uwsgi.log
        exit 1
    else
        source .env/bin/activate
        uwsgi --ini cloudlte_uwsgi.ini
        echo -e "${SETCOLOR_SUCCESS_SHARE}The CloudLte is Start, \n Develop Server at http://${ip_address}:${port}/ ${SETCOLOR_RES}"
        read -n 1 -s
        tail -f .uwsgi.log
        exit 1
    fi
fi

# restart
if [ $1 == 'restart' ]; then
    isrun=`ps -ef|grep [c]loudlte_uwsgi|wc -l`
    if [ $isrun -gt 0 ]; then
        source .env/bin/activate
        uwsgi --stop .uwsgi.pid
        sleep 3
        uwsgi --ini cloudlte_uwsgi.ini
        echo -e "${SETCOLOR_SUCCESS_SHARE}The CloudLte is Start, \n Develop Server at http://${ip_address}:${port}/ ${SETCOLOR_RES}"
        read -n 1 -s
        tail -f .uwsgi.log
        exit 1
    else
        source .env/bin/activate
        uwsgi --ini cloudlte_uwsgi.ini
        echo -e "${SETCOLOR_SUCCESS_SHARE}The CloudLte is Start, \n Develop Server at http://${ip_address}:${port}/ ${SETCOLOR_RES}"
        read -n 1 -s
        tail -f .uwsgi.log
        exit 1
    fi
fi

# stop
if [ $1 == 'stop' ]; then
    source .env/bin/activate
    isrun=`ps -ef|grep [c]loudlte_uwsgi|wc -l`
    if [ $isrun -gt 0 ]; then
        uwsgi --stop .uwsgi.pid
        echo -e "${SETCOLOR_SUCCESS_SHARE}The CLoudLte is Stoped! ${SETCOLOR_RES}"
        exit 1
    else
        echo -e "${SETCOLOR_WARNING}CLoudLte Status: ${SETCOLOR_FAILURE}Stoped. ${SETCOLOR_RES}"
    fi
fi

# status
if [ $1 == 'status' ]; then
    source .env/bin/activate
    isrun=`ps -ef|grep [c]loudlte_uwsgi|wc -l`
    if [ $isrun -gt 0 ]; then
        echo -e "${SETCOLOR_WARNING}CLoudLte Status: ${SETCOLOR_SUCCESS}Starting. ${SETCOLOR_RES}"
        echo -e "Develop Server at http://${ip_address}:${port}/"
    else
        echo -e "${SETCOLOR_WARNING}CLoudLte Status: ${SETCOLOR_FAILURE}Stoped. ${SETCOLOR_RES}"
    fi
fi